package org.traffometer.traffosim;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.File;
import java.util.List;

import org.junit.Test;
import org.traffometer.common.data.TraffoPacket;
import org.traffometer.opendmtp.protocol.GeoPoint;

/**
 * Tests the ConfigWriter class by constructing a static and a dynamic configuration file.
 * 
 * @author George Lee
 * 
 */
public class TestConfigWriter {

  /**Parameters to create TraffoPacket.*/
  private static final double lat1 = 10;
  private static final double long1 = 32.44;
  private static final long time1 = 56778;

  private static final double lat2 = 33.45;
  private static final double long2 = 170.23;
  private static final long time2 = 88883;

  private static final String vehicle = "Foo-Bar";

  private static final GeoPoint point1 = new GeoPoint(lat1, long1, time1);
  private static final GeoPoint point2 = new GeoPoint(lat2, long2, time2);

  private TraffoPacket packet;

  /** Directory path to write the static configuration file to. */
  private static final String STATIC_FILE = "build/static-test.cfg";

  /** Directory path to write the dynamic configuration file to. */
  //private static final String DYNAMIC_FILE = "build/dynamic-test.cfg";
  
  /**
   * Tests the ability of the ConfigWriter to create a static configuration file with the batch
   * transmission mode.
   * 
   * @throws Exception if there is an error in running the test.
   */
  @Test
  @SuppressWarnings("unused")
  public void testStaticConfig() throws Exception {
    ConfigWriter writer = new ConfigWriter("Static", "Batch", 1);

    // Construct a TraffoPacket to pass to the ConfigWriter.
    this.packet = new TraffoPacket(this.point1, this.point2, this.vehicle);

    writer.addPacket(packet);
    writer.save(STATIC_FILE);

    //Attempt to load the configuration file.
    try {
      Config config = new Config(new File(STATIC_FILE));
      
      //Retrieve packet list and check that packet is correctly written.
      List<TraffoPacket> packets = config.getPackets();
      TraffoPacket testPacket = packets.get(0);
      
      assertEquals("Retrieving latitude1 value.", this.lat1, testPacket.getLatitude1());
    }

    catch (ConfigException e) {
      //If there is an exception, fail the test.
      fail("Error in loading the static config file: " + e);
    }
  }

  // Dynamic configuration files not supported by Traffosim yet.
  //
  // Tests the ability of the ConfigWriter to create a dynamic configuration file using the batch
  // transmission mode.
  // 
  // @throws Exception if there is an error during operation.
  //
  //@Test
  //@SuppressWarnings("unused")
  //public void testDynamicConfig() throws Exception {
  //ConfigWriter writer = new ConfigWriter("Dynamic", "Batch", 1);

  //this.packet = new TraffoPacket(this.point1, this.point2, this.vehicle);

  //writer.addPacket(this.packet);
  //writer.save(DYNAMIC_FILE);

  ////Attempt to load the configuration file.
  //try {
  //  Config config = new Config(new File(DYNAMIC_FILE));
  //}

  //catch (ConfigException e) {
  //  fail("Error in loading the dynamic config file: " + e);
  //}
  //}
  
  /**
   * Tests passing  an invalid config type to the ConfigWriter.
   * 
   * @throws ConfigWriterException when bad data is passed (expected to be thrown).
   */
  @Test(expected = ConfigWriterException.class)
  @SuppressWarnings("unused")
  public void testInvalidConfig() throws ConfigWriterException {
    ConfigWriter writer = new ConfigWriter("foo", "batch");
    
    //Pacifies findbugs.
    writer.setConfigType("dynamic");
  }
  
  /**
   * Tests passing an invalid transmission mode to the ConfigWriter.
   * 
   * @throws ConfigWriterException when bad data is passed (expected to be thrown).
   */
  @Test(expected = ConfigWriterException.class)
  @SuppressWarnings("unused")
  public void testInvalidMode() throws ConfigWriterException {
    ConfigWriter writer = new ConfigWriter("static", "bar");
    
    //Pacifies findbugs.
    writer.setConfigType("dynamic");
  }
  
  /**
   * Tests passing an invalid config type to setConfigType.
   * 
   * @throws ConfigWriterException when bad data is passed (expected to be thrown).
   */
  @Test(expected = ConfigWriterException.class)
  @SuppressWarnings("unused")
  public void testInvalidSetConfig() throws ConfigWriterException {
    ConfigWriter writer = new ConfigWriter("static", "batch");
    writer.setConfigType("foo");
  }
  
  /**
   * Tests passing an invalid transmission mode to setTransmissionMode.
   * 
   * @throws ConfigWriterException when bad data is passed (expected to be thrown).
   */
  @Test(expected = ConfigWriterException.class)
  @SuppressWarnings("unused")
  public void testInvalidSetTransmission() throws ConfigWriterException {
    ConfigWriter writer = new ConfigWriter("static", "batch");
    writer.setTransmissionMode("foo");
  }

}
